﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

/// <summary>
/// Summary description for clsSQLOperations
/// </summary>
public class clsSQLOperations
{
    public enum ActionResult {Success=1,Fail=0,NA=-1}

	public clsSQLOperations()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static string GetConnectionString()
    {
        return ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString.ToString();
    }
    public static string GetServerName()
    {
        return ConfigurationManager.AppSettings["SQLServer"].ToString();
    }
    public static ActionResult StartJob(string JobName)
    {
        try
        {
            Server sqlServer = new Server(GetServerName());
            sqlServer.JobServer.Jobs[JobName].Start();            
            return ActionResult.Success;
        }
        catch (Exception)
        {
            return ActionResult.Fail;
        }
    }
    public static ActionResult StopJob(string JobName)
    {
        try
        {
            Server sqlServer = new Server(GetServerName());
            sqlServer.JobServer.Jobs[JobName].Stop();
            return ActionResult.Success;
        }
        catch (Exception)
        {
            return ActionResult.Fail;
        }
    }
    public static DataTable GetJobList()
    {
        Server sqlserver = new Server(GetServerName());
        DataTable tblServerJobs = sqlserver.JobServer.EnumJobs();
        return tblServerJobs;
    }
    public static DataTable GetJobHistory(string JobName)
    {
        Server sqlserver = new Server(GetServerName());
        DataTable Results = sqlserver.JobServer.Jobs[JobName].EnumHistory();
        return Results;
    }
    public static DateTime GetJobLastRunDate(string JobName)
    {
        Server sqlserver = new Server(GetServerName());
        return sqlserver.JobServer.Jobs[JobName].LastRunDate;
    }
    public static string GetJobLastRunResult(string JobName)
    {
        Server sqlserver = new Server(GetServerName());
        return sqlserver.JobServer.Jobs[JobName].LastRunOutcome.ToString();
    }

}